package fr.pud.client.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import fr.pud.client.controller.InputCheck;
import fr.pud.client.controller.query.SuggestionsForJComboBox;
import fr.pud.client.i18n.LanguagesConstants;
import fr.pud.client.model.PUDStationDatabase;
import fr.pud.client.view.jsuggestfield.JSuggestField;
import fr.pud.utils.Tools;

/**
 * 5 avr. 2012 - Projet_Ose
 * @author Simon Devineau, Adrien Hierry, Leopold Houdin, Pierre Reliquet
 *         Ecole des Mines de Nantes
 *         InputQueryDataPanel.java
 *         This class create a JPanel used to get all the data to create a query
 *         as :
 *         - the quantity
 *         - the max drive duration
 *         - the address of the pickup
 *         - the address of the delivery
 *         - the starting and closing time of the delivery and pickup.
 */
public class InputQueryDataPanel extends JPanel {
    /**
     * 
     */
    private static final long serialVersionUID   = 6583919398020180156L;
    /**
     * JTextField where the user will type the quantity of the query.
     */
    private JTextField        textQuantity;
    /**
     * JTextField where the user will type the max drive duration of the query.
     */
    private JTextField        textMaxDrive;
    /**
     * JTextField where the user will input the loading time predicted for the
     * delivery.
     */
    private JTextField        textLoadingTimeDelivery;
    /**
     * JTextField where the user will input the loading time predicted for the
     * pickUp.
     */
    private JTextField        textLoadingTimePickUp;
    /**
     * JTextField where the user will type the opening time of the pickup.
     */
    private JTextField        textOpeningTimePickup;
    /**
     * JTextField where the user will type the opening time of the delivery.
     */
    private JTextField        textOpeningTimeDelivery;
    /**
     * JTextField where the user will type the closing time of the pickup.
     */
    private JTextField        textClosingTimePickup;
    /**
     * JTextField where the user will type the closing time of the delivery.
     */
    private JTextField        textClosingTimeDelivery;
    /**
     * JTextField where the user will type the address of the pickup.
     */
    private JComboBox         addressPickUp;
    /**
     * JTextField where the user will type the address of the delivery.
     */
    private JComboBox         addressDelivery;
    /**
     * The dimension of the JComboBox
     */
    private final Dimension   DIMENSION_COMBOBOX = new Dimension(400, 20);

    /**
     * Create the JPanel using all the method prepare.
     */
    public InputQueryDataPanel(JSuggestField[] arrayOfJSuggestField) {
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        this.preparePanelPUDStation(arrayOfJSuggestField);
        JPanel panelSouthForQueriesData = new JPanel(new FlowLayout(
                FlowLayout.CENTER));
        panelSouthForQueriesData.add(this.preparePanelQuantity());
        panelSouthForQueriesData.add(this.preparePanelMaxDrive());
        panelSouthForQueriesData.setBorder(BorderFactory.createLineBorder(
                Color.BLACK, 1));
        this.add(panelSouthForQueriesData);
    }

    /**
     * @return the addressDelivery
     */
    public JComboBox getPUDStationDelivery() {
        return this.addressDelivery;
    }

    /**
     * @return the addressPickUp
     */
    public JComboBox getPUDStationPickUp() {
        return this.addressPickUp;
    }

    /**
     * @return the textClosingTimeDelivery
     */
    public JTextField getTextClosingTimeDelivery() {
        return this.textClosingTimeDelivery;
    }

    /**
     * @return the textClosingTimePickup
     */
    public JTextField getTextClosingTimePickup() {
        return this.textClosingTimePickup;
    }

    /**
     * @return the textLoadingTimeDelivery
     */
    public JTextField getTextLoadingTimeDelivery() {
        return this.textLoadingTimeDelivery;
    }

    /**
     * @return the textLoadingTimePickUp
     */
    public JTextField getTextLoadingTimePickUp() {
        return this.textLoadingTimePickUp;
    }

    /**
     * @return the textMaxDrive
     */
    public JTextField getTextMaxDrive() {
        return this.textMaxDrive;
    }

    /**
     * @return the textOpeningTimeDelivery
     */
    public JTextField getTextOpeningTimeDelivery() {
        return this.textOpeningTimeDelivery;
    }

    /**
     * @return the textOpeningTimePickup
     */
    public JTextField getTextOpeningTimePickup() {
        return this.textOpeningTimePickup;
    }

    /**
     * @return the textQuantity
     */
    public JTextField getTextQuantity() {
        return this.textQuantity;
    }

    /**
     * Prepare the part concerning the address of the delivery.
     * @return
     */
    private JPanel prepareAddressDelivery() {
        JPanel panelAddressDelivery = new JPanel(new FlowLayout());
        panelAddressDelivery.setLayout(new BoxLayout(panelAddressDelivery,
                BoxLayout.Y_AXIS));
        this.addressDelivery = new JComboBox(PUDStationDatabase.getInstance()
                .getStations().toArray());
        JTextField text = new JTextField(10);
        SuggestionsForJComboBox suggestions = new SuggestionsForJComboBox(text,
                this.addressDelivery);
        text.addKeyListener(suggestions);
        text.addFocusListener(suggestions);
        this.addressDelivery.setPreferredSize(this.DIMENSION_COMBOBOX);
        panelAddressDelivery.add(text);
        panelAddressDelivery.add(this.addressDelivery);
        panelAddressDelivery.setBorder(BorderFactory.createMatteBorder(0, 0, 1,
                1, Color.BLACK));
        return panelAddressDelivery;
    }

    /**
     * Prepare the part concerning the address of the pickup.
     * @return
     */
    private JPanel prepareAddressPickup() {
        JPanel panelAddressDelivery = new JPanel();
        panelAddressDelivery.setLayout(new BoxLayout(panelAddressDelivery,
                BoxLayout.Y_AXIS));
        this.addressPickUp = new JComboBox(PUDStationDatabase.getInstance()
                .getStations().toArray());
        JTextField text = new JTextField(10);
        SuggestionsForJComboBox suggestions = new SuggestionsForJComboBox(text,
                this.addressPickUp);
        text.addKeyListener(suggestions);
        text.addFocusListener(suggestions);
        this.addressPickUp.setPreferredSize(this.DIMENSION_COMBOBOX);
        panelAddressDelivery.add(text);
        panelAddressDelivery.add(this.addressPickUp);
        panelAddressDelivery.setBorder(BorderFactory.createMatteBorder(0, 1, 1,
                1, Color.BLACK));
        return panelAddressDelivery;
    }

    /**
     * Create label and add to a JPanel the text Delivery
     */
    private JLabel prepareLabelForDelivery() {
        JLabel delivery = new JLabel(
                LanguagesConstants.LANGUAGE
                        .getString(LanguagesConstants.DELIVERY_STATION));
        delivery.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0,
                Color.BLACK));
        return delivery;
    }

    /**
     * Create label and add to a JPanel the text Pickup.
     */
    private JLabel prepareLabelForPickUp() {
        JLabel pickup = new JLabel(
                LanguagesConstants.LANGUAGE
                        .getString(LanguagesConstants.PICK_UP_STATION));
        pickup.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 1,
                Color.BLACK));
        return pickup;
    }

    /**
     * Prepare the part concerning the closing time of the delivery.
     * @return
     */
    private JPanel preparePanelClosingTimeDelivery(
            JSuggestField textClosingTimeDelivery) {
        JPanel panelClosingTime = new JPanel(new FlowLayout(FlowLayout.LEFT));
        if (Tools.isPlatformLinux()) {
            this.textClosingTimeDelivery = new JTextField();
        }
        else {
            this.textClosingTimeDelivery = textClosingTimeDelivery;
        }
        this.textClosingTimeDelivery.setColumns(5);
        InputCheck inputCheckHours = new InputCheck(
                this.textClosingTimeDelivery, InputCheck.HOURS);
        this.textClosingTimeDelivery.addKeyListener(inputCheckHours);
        this.textClosingTimeDelivery.addFocusListener(inputCheckHours);
        panelClosingTime.add(this.textClosingTimeDelivery);
        return panelClosingTime;
    }

    /**
     * Prepare the part concerning the closing time of the pickup.
     * @return
     */
    private JPanel preparePanelClosingTimePickup(
            JSuggestField textClosingTimePickup) {
        JPanel panelClosingTime = new JPanel(new FlowLayout(FlowLayout.LEFT));
        if (Tools.isPlatformLinux()) {
            this.textClosingTimePickup = new JTextField();
        }
        else {
            this.textClosingTimePickup = textClosingTimePickup;
        }
        this.textClosingTimePickup.setColumns(5);
        InputCheck inputCheckHours = new InputCheck(this.textClosingTimePickup,
                InputCheck.HOURS);
        this.textClosingTimePickup.addKeyListener(inputCheckHours);
        this.textClosingTimePickup.addFocusListener(inputCheckHours);
        panelClosingTime.add(this.textClosingTimePickup);
        panelClosingTime.setBorder(BorderFactory.createMatteBorder(0, 1, 0, 1,
                Color.BLACK));
        return panelClosingTime;
    }

    /**
     * Prepare the part concerning the loading time for the pickUp.
     */
    private JPanel preparePanelLoadingTimeDelivery() {
        JPanel panLoadingTime = new JPanel(new FlowLayout(FlowLayout.LEFT));
        this.textLoadingTimeDelivery = new JTextField(2);
        this.textLoadingTimeDelivery.addKeyListener(new InputCheck(
                this.textLoadingTimeDelivery, InputCheck.NUMBER_ONLY));
        panLoadingTime.add(this.textLoadingTimeDelivery);
        panLoadingTime.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0,
                Color.BLACK));
        return panLoadingTime;
    }

    /**
     * Prepare the part concerning the loading time for the pickUp.
     */
    private JPanel preparePanelLoadingTimePickup() {
        JPanel panelLoadingTime = new JPanel(new FlowLayout(FlowLayout.LEFT));
        this.textLoadingTimePickUp = new JTextField(2);
        this.textLoadingTimePickUp.addKeyListener(new InputCheck(
                this.textLoadingTimePickUp, InputCheck.NUMBER_ONLY));
        panelLoadingTime.add(this.textLoadingTimePickUp);
        panelLoadingTime.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 1,
                Color.BLACK));
        return panelLoadingTime;
    }

    /**
     * Prepare the part concerning the max drive duration.
     */
    private JPanel preparePanelMaxDrive() {
        JPanel panelMaxDrive = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JLabel lab = new JLabel(
                LanguagesConstants.LANGUAGE
                        .getString(LanguagesConstants.MAX_DRIVE_DURATION)
                        + " : ");
        this.textMaxDrive = new JTextField(2);
        this.textMaxDrive.addKeyListener(new InputCheck(this.textMaxDrive,
                InputCheck.NUMBER_ONLY));
        panelMaxDrive.add(lab);
        panelMaxDrive.add(this.textMaxDrive);
        return panelMaxDrive;
    }

    /**
     * Prepare the part concerning the opening time of the delivery.
     * @return
     */
    private JPanel preparePanelOpeningTimeDelivery(
            JSuggestField textOpeningTimeDelivery) {
        JPanel panelOpeningTime = new JPanel(new FlowLayout(FlowLayout.LEFT));
        if (Tools.isPlatformLinux()) {
            this.textOpeningTimeDelivery = new JTextField();
        }
        else {
            this.textOpeningTimeDelivery = textOpeningTimeDelivery;
        }
        this.textOpeningTimeDelivery.setColumns(5);
        InputCheck inputCheckHours = new InputCheck(
                this.textOpeningTimeDelivery, InputCheck.HOURS);
        this.textOpeningTimeDelivery.addKeyListener(inputCheckHours);
        this.textOpeningTimeDelivery.addFocusListener(inputCheckHours);
        panelOpeningTime.add(this.textOpeningTimeDelivery);
        panelOpeningTime.setBorder(BorderFactory.createMatteBorder(0, 1, 1, 0,
                Color.BLACK));
        return panelOpeningTime;
    }

    /**
     * Prepare the part concerning the opening time of the pickup.
     * @return
     */
    private JPanel preparePanelOpeningTimePickup(JSuggestField openingTime) {
        JPanel panelOpeningTime = new JPanel(new FlowLayout(FlowLayout.LEFT));
        if (Tools.isPlatformLinux()) {
            this.textOpeningTimePickup = new JTextField();
        }
        else {
            this.textOpeningTimePickup = openingTime;
        }
        this.textOpeningTimePickup.setColumns(5);
        InputCheck inputCheckHours = new InputCheck(this.textOpeningTimePickup,
                InputCheck.HOURS);
        this.textOpeningTimePickup.addKeyListener(inputCheckHours);
        this.textOpeningTimePickup.addFocusListener(inputCheckHours);
        panelOpeningTime.add(this.textOpeningTimePickup);
        panelOpeningTime.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0,
                Color.BLACK));
        return panelOpeningTime;
    }

    /**
     * Prepare the JPabel where all the data will be found to create a
     * PUDStation.
     */
    private void preparePanelPUDStation(JSuggestField[] arrayOfJSuggestField) {
        GridLayout layout = new GridLayout(5, 2);
        JPanel panelPUDStation = new JPanel(layout);
        JLabel stations = new JLabel(
                LanguagesConstants.LANGUAGE
                        .getString(LanguagesConstants.STATION_NAME) + " : ");
        stations.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 1,
                Color.BLACK));
        panelPUDStation.add(stations);
        panelPUDStation.add(this.prepareLabelForPickUp());
        panelPUDStation.add(this.prepareLabelForDelivery());
        panelPUDStation.add(new JLabel(LanguagesConstants.LANGUAGE
                .getString(LanguagesConstants.STATION_ADDRESS) + " : "));
        panelPUDStation.add(this.prepareAddressPickup());
        panelPUDStation.add(this.prepareAddressDelivery());
        JLabel loadingTimes = new JLabel(
                LanguagesConstants.LANGUAGE
                        .getString(LanguagesConstants.STATION_LOADING_TIME)
                        + " (min) :");
        loadingTimes.setBorder(BorderFactory.createMatteBorder(1, 0, 1, 1,
                Color.BLACK));
        panelPUDStation.add(loadingTimes);
        panelPUDStation.add(this.preparePanelLoadingTimePickup());
        panelPUDStation.add(this.preparePanelLoadingTimeDelivery());
        JLabel openingTimes = new JLabel(
                LanguagesConstants.LANGUAGE
                        .getString(LanguagesConstants.STATION_OPENING) + " : ");
        openingTimes.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 1,
                Color.BLACK));
        panelPUDStation.add(openingTimes);
        panelPUDStation.add(this
                .preparePanelOpeningTimePickup(arrayOfJSuggestField[0]));
        panelPUDStation.add(this
                .preparePanelOpeningTimeDelivery(arrayOfJSuggestField[1]));
        JLabel closingTimes = new JLabel(
                LanguagesConstants.LANGUAGE
                        .getString(LanguagesConstants.STATION_CLOSING) + " : ");
        panelPUDStation.setBorder(BorderFactory
                .createLineBorder(Color.BLACK, 1));
        panelPUDStation.add(closingTimes);
        panelPUDStation.add(this
                .preparePanelClosingTimePickup(arrayOfJSuggestField[2]));
        panelPUDStation.add(this
                .preparePanelClosingTimeDelivery(arrayOfJSuggestField[3]));
        this.add(panelPUDStation);
    }

    /**
     * Prepare the part concerning the quantity.
     */
    private JPanel preparePanelQuantity() {
        JPanel panelQuantity = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JLabel labelQuantity = new JLabel(
                LanguagesConstants.LANGUAGE
                        .getString(LanguagesConstants.STATION_QUANTITY_REQUIRED)
                        + " : ");
        this.textQuantity = new JTextField(2);
        this.textQuantity.addKeyListener(new InputCheck(this.textQuantity,
                InputCheck.NUMBER_ONLY));
        panelQuantity.add(labelQuantity);
        panelQuantity.add(this.textQuantity);
        return panelQuantity;
    }

    /**
     * @param aAddressDelivery
     *            the addressDelivery to set
     */
    public void setAddressDelivery(JComboBox aAddressDelivery) {
        this.addressDelivery = aAddressDelivery;
    }

    /**
     * @param aAddressPickUp
     *            the addressPickUp to set
     */
    public void setAddressPickUp(JComboBox aAddressPickUp) {
        this.addressPickUp = aAddressPickUp;
    }

    /**
     * @param aTextClosingTimeDelivery
     *            the textClosingTimeDelivery to set
     */
    public void setTextClosingTimeDelivery(JTextField aTextClosingTimeDelivery) {
        this.textClosingTimeDelivery = aTextClosingTimeDelivery;
    }

    /**
     * @param aTextClosingTimePickup
     *            the textClosingTimePickup to set
     */
    public void setTextClosingTimePickup(JTextField aTextClosingTimePickup) {
        this.textClosingTimePickup = aTextClosingTimePickup;
    }

    /**
     * @param aTextOpeningTimeDelivery
     *            the textOpeningTimeDelivery to set
     */
    public void setTextOpeningTimeDelivery(JTextField aTextOpeningTimeDelivery) {
        this.textOpeningTimeDelivery = aTextOpeningTimeDelivery;
    }

    /**
     * @param aTextOpeningTimePickup
     *            the textOpeningTimePickup to set
     */
    public void setTextOpeningTimePickup(JSuggestField aTextOpeningTimePickup) {
        this.textOpeningTimePickup = aTextOpeningTimePickup;
    }
}
